<template>
  <div class="box">
    <!-- 学科 -->
    <div style="margin-bottom: 10px">
      <span>目录名称</span>
      <el-input v-model="name" class="inp" placeholder="请输入内容" clearable />
      <span>状态</span>
      <el-select v-model="input" placeholder="请选择活动区域" class="inp">
        <el-option label="启用" value="1" />
        <el-option label="禁用" value="0" />
      </el-select>
      <el-button @click="qingchu">清除</el-button
      ><el-button type="primary" @click="sousuo">搜索</el-button>

      <el-button
        type="success"
        icon="el-icon-edit"
        class="adds"
        @click="xinzheng"
      >
        新增目录
      </el-button>
      <el-button
        v-show="fanhui"
        type="text"
        icon="el-icon-back"
        class="adds"
        @click="$router.go(-1)"
      >
        返回学科</el-button
      >
    </div>
    <!-- 提示 -->
    <el-alert type="info" show-icon>
      <template #title> 数据一共 {{ numberOfArticles }} </template>
    </el-alert>
    <!-- 表格 -->
    <el-table
      ref="singleTable"
      :data="tableData"
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column property="subjectName" label="所属学科" width="120" />
      <el-table-column property="directoryName" label="目录名称" width="120" />
      <el-table-column property="username" label="创建者" width="120" />
      <el-table-column property="addDate" label="创建日期" width="120">
        <template v-slot="{ row }">
          {{ row.addDate | times }}
        </template>
      </el-table-column>
      <el-table-column property="totals" label="面试题目" width="120" />
      <el-table-column property="state" label="状态" width="120">
        <template v-slot="{ row }">
          {{ row.state === 1 ? '已启用' : '已禁用' }}
        </template>
      </el-table-column>
      <el-table-column property="address" label="操作">
        <template v-slot="{ row }">
          <el-button v-if="row.state === 1" type="text" @click="jinyong(row)">
            禁用
          </el-button>
          <el-button v-else type="text" @click="qiyong(row)"> 启用 </el-button>
          <span v-if="row.state === 1">
            <button ref="xiugai" type="text" class="anniu" @click="revise(row)">
              修改
            </button>
            <button ref="dell" type="text" class="anniu" @click="dell(row)">
              删除
            </button>
          </span>
          <span v-else>
            <button style="color: #999" type="text" class="anniu">修改</button>
            <button style="color: #999" type="text" class="anniu">删除</button>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      style="float: right"
      background
      :current-page="page"
      :page-sizes="[5, 10, 20, 50, 100]"
      :page-size="pagesize"
      layout="  prev, pager, next,sizes, jumper"
      :total="numberOfArticles"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 弹窗添加修改 -->
    <el-dialog :visible.sync="dialogVisible" width="30%">
      <template #title>
        <span v-if="show">新增目录</span>
        <span v-else>修改目录</span>
      </template>
      <div v-show="id === undefined">
        <span><i style="color: red; margin: 8px">*</i>所属学科</span>
        <el-select
          v-model="region"
          placeholder="请选择活动区域"
          style="width: 70%; margin-left: 10px"
        >
          <el-option
            v-for="item in arr"
            :key="item.id"
            :label="item.subjectName"
            :value="item.id"
          />
        </el-select>
      </div>
      <br /><br />
      <span><i style="color: red; margin: 8px">*</i>目录名称</span>
      <el-input
        v-model="mingcheng"
        style="width: 70%; margin-left: 10px"
        placeholder="请输入学科名称"
      />

      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取 消</el-button>
        <el-button type="primary" @click="addtianjai"> 确 定 </el-button>
      </span>
    </el-dialog>
    <!-- 删除弹框 -->
    <el-dialog title="提示" :visible.sync="delenr" width="30%">
      <span>
        <i class="el-icon-info" style="color: #e6a23c; font-size: 20px" />
        此操作将永久删除该文件，是否继续
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delenr = false">取 消</el-button>
        <el-button type="primary" @click="dels"> 确 定 </el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  mubiaolist,
  mubiaolzhuangtai,
  xuekelist,
  muluadd,
  muluxiugai,
  muludel
} from '@/api/subject'
export default {
  data() {
    return {
      name: '', // 搜索名字
      mingcheng: '', // 目录名称
      arr: [],
      fanhui: false, // 返回按钮
      id: this.$route.query.id, // 接口请求id
      delId: '', // 删除携带的id
      delenr: false, // 删除弹框
      region: '', // 选择学科
      // bufen: true, // 禁用启用按钮
      // value: true, // 是否显示弹框里的
      show: true, // 判断是否是新增
      dialogVisible: false, // 弹框显示或隐藏
      pagesize: 10, // 每页几条
      page: 1, // 当前页
      input: '', // 输入框
      numberOfArticles: 100, // 数据总条数
      // 渲染的数据
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }
      ]
    }
  },
  created() {
    this.renderThePage()
    // console.log(this.$route.query.id)
  },
  mounted() {},
  methods: {
    // 取消
    quxiao() {
      this.dialogVisible = false
      this.region = ''
      this.mingcheng = ''
    },
    // 清除
    qingchu() {
      this.name = ''
      this.input = ''
      this.renderThePage()
    },
    // 搜索
    async sousuo() {
      if (this.name !== '' && this.input !== '') {
        const { data } = await mubiaolist({
          page: this.page,
          pagesize: this.pagesize,
          directoryName: this.name,
          state: Number(this.input)
        })
        this.numberOfArticles = data.counts
        this.tableData = data.items
      }
    },
    // 添加
    async addtianjai() {
      if (this.show) {
        if (this.id === undefined) {
          await muluadd({
            subjectID: this.region,
            directoryName: this.mingcheng
          })
        } else {
          await muluadd({
            subjectID: this.$route.query.id,
            directoryName: this.mingcheng
          })
        }
      } else {
        await muluxiugai({
          id: this.delId,
          subjectID: this.region,
          directoryName: this.mingcheng
        })
      }
      this.dialogVisible = false
      this.renderThePage()
      this.region = ''
      this.mingcheng = ''
    },
    // 新增目录
    async xinzheng() {
      this.show = true
      this.dialogVisible = true
      const { data } = await xuekelist()
      console.log(data.items)
      this.arr = data.items
    },
    // 启用按钮
    async jinyong(row) {
      await mubiaolzhuangtai({
        id: row.id,
        state: 0
      })
      this.renderThePage()
    },
    // 禁用按钮
    async qiyong(row) {
      await mubiaolzhuangtai({
        id: row.id,
        state: 1
      })
      this.renderThePage()
    },
    // 渲染页面
    async renderThePage() {
      if (this.id === undefined) {
        const { data } = await mubiaolist({
          page: this.page,
          pagesize: this.pagesize
        })
        this.numberOfArticles = data.counts
        this.tableData = data.items
        console.log(data)
      } else {
        this.fanhui = true
        const { data } = await mubiaolist({
          page: this.page,
          pagesize: this.pagesize,
          subjectID: this.id
        })
        this.numberOfArticles = data.counts
        this.tableData = data.items
        console.log(data)
      }
    },
    // 点击修改
    async revise(row) {
      // console.log(row)
      this.show = false
      this.dialogVisible = true
      const { data } = await xuekelist()
      // console.log(data.items)
      this.arr = data.items
      this.region = row.subjectID
      this.mingcheng = row.directoryName
      this.delId = row.id
    },
    // 点击删除
    dell(row) {
      console.log(row)
      this.delenr = true
      this.delId = row.id
    },
    // 确认删除
    async dels() {
      await muludel(this.delId)
      this.delenr = false
      this.renderThePage()
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.page = val
      this.renderThePage()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.renderThePage()
    }
  }
}
</script>
<style scoped lang="scss">
.anniu {
  border: 0;
  background-color: transparent;
  font-size: 15px;
  color: #409eff;
}
.adds {
  float: right;
}
.box {
  padding: 20px;
  /* background-color: #f4f4f5; */
}
.inp {
  width: 200px;
  margin: 0 10px;
}
</style>
